package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// 类型实例化变量赋值调用策略
/**
 *  a=new("type")
 *  a.name=123
 */
type InstancePropAssignmentStrategy struct {
}

// 注册策略
func init() {
	RegisterStrategy(&InstancePropAssignmentStrategy{})
}

// 判断
func (f *InstancePropAssignmentStrategy) getId() int {
	return lineanalysis.InstancePropAssignment
}

func (f *InstancePropAssignmentStrategy) Execute(threadStore *structdef.ThreadStore) {
	content := threadStore.Code.LineContent
	split := strings.Split(content, "=")
	//获取=号左边的变量
	variableName := split[0]
	//获取等号右边的表达式
	expression := split[1]
	//获取实例变量
	index1 := strings.Index(variableName, ".")
	varName := variableName[:index1]    //实例变量名称
	propName := variableName[index1+1:] //属性名称
	addr, _ := store.GetInstance(threadStore, varName)
	//解析表达式
	calculateExpression := globalfunc.ExecuteFunc("AssignmentStrategyMode", threadStore, expression).(string)
	var variableType = globalfunc.ExecuteFunc("CodeAnalysis", threadStore, calculateExpression).(int)
	store.SetInstanceProp(threadStore, addr, structdef.NewVarDefinitionValue(propName, calculateExpression, variableType))
}
